﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace ICU
{
    public class CSharpPlugin : Plugin
    {
        public DirectoryInfo DataFolder;
        public IServer Server;
        public string path;
        public IServer.Logger logger;
        private bool disposedValue;

        public virtual string Name { get; }
        public virtual string Description { get; }
        public virtual string[] Author { get; }
        public virtual Version Version { get; }
        public virtual string HomePage { get; }
        public virtual string UpdateUrl { get; }
        public virtual string[] Commands { get; }

        public CSharpPlugin(IServer server, string path)
        {
            logger = server.GetLogger(Name);
            Server = server;
            this.path = path;
            DataFolder = new DirectoryInfo(string.Format("{0}{1}data{1}{2}", Environment.CurrentDirectory, Path.DirectorySeparatorChar, Name));
            if (!DataFolder.Exists)
            {
                DataFolder.Create();
            }
            string author = "";
            foreach (var item in Author)
            {
                author += item;
            }
            logger.Info($"Plugin information:");
            logger.Info($"  Name: {Name}");
            logger.Info($"  Author: [{author}]");
            logger.Info($"  Version: {Version}");
            logger.Info($"  Description: {Description??""}");
            logger.Info($"  Home Page: {HomePage??""}");
        }

        public event EventHandler<UserSendMessageEventArgs> UserSendMessageEvent { add { Server.UserSendMessageEvent += (s,e) => { value(s, e); }; } remove { Server.UserSendMessageEvent -= value; } }
        public event EventHandler<UserSendGroupMessageEventArgs> UserSendGroupMessageEvent { add { Server.UserSendGroupMessageEvent += value; } remove { Server.UserSendGroupMessageEvent -= value; } }
        public event EventHandler<UserSendUserMessageEventArgs> UserSendUserMessageEvent { add { Server.UserSendUserMessageEvent += value; } remove { Server.UserSendUserMessageEvent -= value; } }
        public event EventHandler<UserLoginEventArgs> UserLoginEvent { add { Server.UserLoginEvent += value; } remove { Server.UserLoginEvent -= value; } }
        public event EventHandler<UserLogoutEventArgs> UserLogoutEvent { add { Server.UserLogoutEvent += value; } remove { Server.UserLogoutEvent -= value; } }
        public event EventHandler<PluginEnableEventArgs> PluginEnableEvent { add { Server.PluginEnableEvent += value; } remove { Server.PluginEnableEvent -= value; } }
        public event EventHandler<PluginDisableEventArgs> PluginDisableEvent { add { Server.PluginDisableEvent += value; } remove { Server.PluginDisableEvent -= value; } }

        public virtual IServer.Logger GetLogger()
        {
            return logger;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    logger = null;
                    Server = null;
                    DataFolder = null;
                    path = null;
                    
                }

                // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~CSharpPlugin()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
}
